﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using IDAL;
using Newtonsoft.Json;

namespace DAL
{
    public class D_ExperienceDetail : IExperienceDetail
    {
        WishDayEntities _e;
        public D_ExperienceDetail()
        {
            _e = new WishDayEntities();
        }
        public void Add(ExperienceDetail obj)
        {
            _e.ExperienceDetail.Add(obj);
            _e.SaveChanges();
        }

        public void Delete(int id)
        {
            _e.ExperienceDetail.Remove(_e.ExperienceDetail.Find(id));
            _e.SaveChanges();
        }

        public ExperienceDetail FindById(int id)
        {
            return _e.ExperienceDetail.Find(id);
        }

        public List<ExperienceDetail> GetAll()
        {
            return _e.ExperienceDetail.ToList();
        }

        public List<Param_Experience> GetDataByTemplate(int id)
        {
            var details = _e.ExperienceDetail.AsEnumerable();
            var type = _e.ExperienceType.AsEnumerable();
            var data = from ed in details
                       join t in type on ed.ETypeID equals t.ETypeID
                       where ed.TemplateID == id
                       orderby t.EPower ascending
                       group ed by t into g
                       select new Param_Experience
                       {
                           ETypeID = g.Key.ETypeID,
                           EName = g.Key.EName,
                           EPower = g.Key.EPower,
                           IsShow = g.Key.IsShow,
                           ListExperienceDetail = (from d in g
                                                   select new Param_ExperienceDetail
                                                   {
                                                       EDID = d.EDID,
                                                       EDOne = d.EDOne,
                                                       EDTwo = d.EDTwo,
                                                       EDThressTime = d.EDThressTime,
                                                       EDThressOutTime = d.EDThressOutTime,
                                                       EDFour = d.EDFour,
                                                       EDFiveDescribe = d.EDFiveDescribe,
                                                       ETypeID = d.ETypeID,
                                                       IsOnTrue = d.IsOnTrue,
                                                       TemplateID = d.TemplateID
                                                   }).ToList()


                       };
            return data.ToList();
        }

        public void Update(int id, ExperienceDetail obj)
        {
            var data = _e.ExperienceDetail.Find(id);
            data.EDOne = obj.EDOne;
            data.EDTwo = obj.EDTwo;
            data.EDThressTime = obj.EDThressTime;
            data.EDThressOutTime = obj.EDThressOutTime;
            data.EDFour = obj.EDFour;
            data.EDFiveDescribe = obj.EDFiveDescribe;
            data.ETypeID = obj.ETypeID;
            data.IsOnTrue = obj.IsOnTrue;
            data.TemplateID = obj.TemplateID;
            _e.SaveChanges();
        }
    }
}
